package com.docking.heylo.web.controller;

import com.docking.heylo.constant.SystemConstant;
import com.docking.heylo.dao.entity.UserBank;
import com.docking.heylo.service.IAccountService;
import com.docking.heylo.utils.HeyloMessageSource;
import com.docking.heylo.web.dto.UserBankDto;
import com.docking.heylo.web.dto.WithdrawDto;
import com.docking.heylo.web.utils.ResultUtil;
import com.docking.heylo.web.vo.BaseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/***
 * 用户账户控制器
 *
 */
@RestController
@RequestMapping("/user/account")
public class UserAccountController {

    @Autowired
    private HeyloMessageSource ms;

    @Autowired
    private IAccountService accountService;


    /***
     * 查看用户账户余额
     *
     * @return
     */
    @RequestMapping("/info")
    public BaseVo info(){
        Map<String, Object> info = accountService.info();
        return ResultUtil.success(ms.getMessage(SystemConstant.success), info);
    }


    /***
     * 银行卡列表
     *
     * @return
     */
    @PostMapping("/bankCardList")
    public BaseVo bankCardList(){
        List<UserBank> userBanks = accountService.userBanks();
        return ResultUtil.success(ms.getMessage(SystemConstant.success), userBanks);
    }

    /***
     * 新增银行卡
     * @return
     */
    @PostMapping("/saveBank")
    public BaseVo saveBank(@RequestBody @Validated(UserBankDto.Sava.class) UserBankDto userBankDto){
        accountService.saveBank(userBankDto);
        return ResultUtil.success(ms.getMessage(SystemConstant.success));
    }

    /***
     * 移除银行卡
     *
     * @return
     */
    @PostMapping("/removeBankCard")
    public BaseVo removeBankCard(@RequestBody @Validated(UserBankDto.IdNotNull.class) UserBank userBank){
        accountService.removeBankCard(userBank);
        return ResultUtil.success(ms.getMessage(SystemConstant.success));
    }

    /***
     * 提现
     *
     * @return
     */
    @PostMapping("/withdraw")
    public BaseVo withdraw(@RequestBody @Validated WithdrawDto withdrawDto){
        accountService.withdraw(withdrawDto);
        return ResultUtil.success(ms.getMessage("withdraw.apply.success"));
    }

    /***
     * 账户记录
     *
     * @return
     */
    @PostMapping("/historyRecord")
    public BaseVo historyRecord(@RequestBody WithdrawDto withdrawDto){
        PageInfo<Map<String, Object>> history = accountService.historyRecord(withdrawDto);
        return ResultUtil.success(ms.getMessage(SystemConstant.success), history);
    }








}
